package basicobject

import (
	"yizhuoyan/go-study/pdfgo/handyio"
)

const referenceKeyWordByte byte = 'R'

type IndirectObjectReference struct {
	ObjectNumber        int
	ObjectVersionNumber int
}

func (this *IndirectObjectReference) Serialize(w handyio.PdfRawWriter) error {
	w.WriteInt(this.ObjectNumber)
	w.WriteByte(' ')
	w.WriteInt(this.ObjectVersionNumber)
	w.WriteByte(' ')
	w.WriteByte(referenceKeyWordByte)
	return nil
}

func (this *IndirectObjectReference) UnSerialize(r handyio.PdfRawReader) error {
	startAt := r.IndexCurrent()
	noSignInt64, err := UnSerializeNoSignInt64(r)
	if err != nil {
		_ = r.IndexMoveTo(startAt)
		return r.NotSupport("无ObjectNumber")
	}
	this.ObjectNumber = noSignInt64
	if !r.NextByteEquals(' ') {
		_ = r.IndexMoveTo(startAt)
		return r.NotSupport("无ObjectNumber间隔")
	}
	gnSignInt64, err := UnSerializeNoSignInt64(r)
	if err != nil {
		_ = r.IndexMoveTo(startAt)
		return r.NotSupport("无ObjectVersionNumber")
	}
	this.ObjectVersionNumber = gnSignInt64
	if !r.NextByteEquals(' ') {
		_ = r.IndexMoveTo(startAt)
		return r.NotSupport("无ObjectVersionNumber间隔")
	}
	if !r.NextByteEquals(referenceKeyWordByte) {
		_ = r.IndexMoveTo(startAt)
		return r.NotSupport("无R")
	}
	return nil

}
